#import "BluetoothPlugin.h"

//控制台是否输出日志
static BOOL LogEnable = NO;
#define DLog(format,...)  if(LogEnable) {\
NSLog((@"| Bluetooth | Flutter | %s[%d]" format), __FUNCTION__, __LINE__, ##__VA_ARGS__);\
}

@interface BluetoothPlugin()<BluetoothManagerDelegate>

@end

@implementation BluetoothPlugin
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
    
    NSObject<FlutterBinaryMessenger>* message = [registrar messenger];
    FlutterMethodChannel* channel = [FlutterMethodChannel
      methodChannelWithName:@"com.jacky.util/blue"
            binaryMessenger:message];
    
    BluetoothPlugin* instance = [[BluetoothPlugin alloc] init];
    instance.channel = channel;
    [registrar addMethodCallDelegate:instance channel:channel];
    
    [BluetoothManager sharedInstance].delegate = instance;
}

- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSString* method = call.method;
    
    if([@"start" isEqualToString:method]) {
        [[BluetoothManager sharedInstance] start];
        BOOL enable = call.arguments[@"showLog"];
        LogEnable = enable;
        DLog(@"开启日志输出");
        result([NSNumber numberWithBool:YES]);
    } else if ([@"startWithAutoConnect" isEqual:method]) {
        BOOL enable = call.arguments[@"showLog"];
        LogEnable = enable;
        NSString *name = call.arguments[@"name"];
        [[BluetoothManager sharedInstance] startWithAutoConnect:name];
        DLog(@"开启日志输出");
        result([NSNumber numberWithBool:YES]);
    } else if([@"stop" isEqualToString:method]) {
        [[BluetoothManager sharedInstance] stop];
        DLog(@"关闭扫描");
    } else if([@"reStartScan" isEqualToString:method]) {
        [[BluetoothManager sharedInstance] restarScan];
        DLog(@"重启扫描");
    }  else if([@"connectPeripheral" isEqualToString:call.method]) {
        [self connectPeripheral:call result:result];
    } else if ([@"getStatus" isEqualToString:call.method]){
        result([NSNumber numberWithInteger:[[BluetoothManager sharedInstance] getStatus]]);
    } else {
        result(FlutterMethodNotImplemented);
    }
}

//连接蓝牙
- (void)connectPeripheral:(FlutterMethodCall*)call result:(FlutterResult)result {
    NSString *name = call.arguments;
    [[BluetoothManager sharedInstance] connectPeripheral:name];
}

//是否输出日志
- (void)setLogEnable:(FlutterMethodCall*)call result:(FlutterResult)result {
    BOOL enable = call.arguments;
    LogEnable = enable;
    DLog(@"开启日志输出");
}

#pragma mark - BluetoothManagerDelegate
- (void)didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI {
    DLog(@"扫描到设备:%@, RSSI:%@",peripheral.name,RSSI);
    NSDictionary *dic = @{@"name":peripheral.name};
    [_channel invokeMethod:@"onDiscoverPeripheral" arguments:dic];
}

- (void)didConnectPeripheral:(CBPeripheral *)peripheral {
    DLog(@"设备连接成功:%@",peripheral.name);
}

- (void)didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    DLog(@"设备连接失败:%@",peripheral.name);
}

- (void)didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error {
    DLog(@"设备断开:%@",peripheral.name);
}

- (void)didDiscoverServices:(NSError *)error {
    DLog(@"服务:%@",error);
}

- (void)didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {
    NSString *jsonStr = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
    DLog(@"特征变化:%@",jsonStr);
    [_channel invokeMethod:@"dataCallback" arguments:(jsonStr)];
}

- (void)didUpdateState:(NSInteger)state {
    [_channel invokeMethod:@"onUpdateState" arguments:@(state)];
}

@end
